package com.ruoyi.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Calendar;
/**
 * 时间工具类
 * 
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    
    /**
     * 获取时间
     *
     * @param num +今天后某天  -今天前某几天 0代表当天
     * @return
     */
    public static String getNextDateStr(int num) {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, num);
        date = cal.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }
    
    /**
     * 获取时间
     *
     * @param num +今天后某天  -今天前某几天
     * @return
     */
    public static String getNextDateStr(String day, int num) throws Exception{
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = formatter.parse(day);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, num);
        date = cal.getTime();
        String dateString = formatter.format(date);
        return dateString;
    }
    
    
    
    /**
     * 获取时间加上时分秒
     *
     * @param num +今天后某天  -今天前某几天
     * @return
     */
    public static String getNextDateStrAddTime(int num) {
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 		Date date=new Date();
 		Calendar calendar = Calendar.getInstance();
 		calendar.setTime(date);
 		calendar.add(Calendar.DAY_OF_MONTH, num);
 		date = calendar.getTime();
 		return sdf.format(date);
	}
    
    
    
    
    /**
     * 获得当前时间后一年的时间
     * @param date  当前时间
     * @return
     * 
     */
    public static Date getYearAfter(Date date) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	calendar.add(Calendar.YEAR, 1);
    	return calendar.getTime();
    }

    
    /**
     * 获取本年第一天
     *
     * @return
     */
    public static String getYear() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        String dateString = formatter.format(date) + "-01-01";
        return dateString;
    }
    
    /**
     * 获取本月第一天
     *
     * @return
     */
    public static String getMonth() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(date) + "-01";
        return dateString;
    }
    
    /**
     * 获取两个时间相差的天数
     *
     * @param BT
     * @param ET
     * @return
     * @throws ParseException
     */
    public static long getDiffDay(String BT, String ET) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(BT);
        Date endTime = sdf.parse(ET);
        long l = endTime.getTime() - startTime.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
	 * 格式化处理时间
	 * 
	 * @param date
	 * @param format 例如 yy-mm-dd
	 * @return
	 */
	public static String formatDate(String date, String format) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			result = sdf.format(date);
		}
		return result;
	}
	
	/**
	 * 获得用户活跃度
	 * @param res
	 * @return
	 */
	public static String formetaDiff(Date date) {
		int days = differentDays(date);
		String res = "";
		int week = 7;
		if (days <= 1) {
			res = "近一天";
		} else if (days == 2) {
			res = "近两天";
		} else if (days == 3) {
			res = "近三天";
		} else if (days <= week) {
			res = "近一周";
		} else if (days > week && days <= week * 2) {
			res = "近两周";
		} else if (days > week * 2 && days <= week * 3) {
			res = "近三周";
		} else if (days > week * 3 && days <= week * 4) {
			res = "近一月";
		} else if (days > week * 4 && days <= week * 8) {
			res = "近两月";
		} else if (days > week * 8 && days <= week * 12) {
			res = "近两月";
		} else if (days > week * 12 && days <= week * 16) {
			res = "近三月";
		} else if (days > week * 16 && days <= week * 24) {
			res = "近半年";
		} else if (days > week * 24 && days <= week * 48) {
			res = "近一年";
		} else {
			res = "一年前";
		}
		return res;
	}
    
	/**
	 * 通过时间秒毫秒数判断指定时间和当前时间的间隔天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date) {
		int days = (int) ((new Date().getTime() - date.getTime()) / (1000 * 3600 * 24));
		return days;
	}
	
	 /**
      *将长时间格式时间转换为字符串
	    * 
	    * @param dateDate  时间
	    * @param String format转换格式  （例如： yyyy-MM-dd HH:mm:ss）
	   * @return
	    */
	 public static String dateToStrLong(java.util.Date date,String format) {
	    SimpleDateFormat formatter = new SimpleDateFormat(format);
	    String dateString = formatter.format(date);
	    return dateString;
	 }
	
	 /**
	  *   获取精确到秒的时间戳 
	  	@param date 
	  	@return 
	  *  
	  */
	public static int getSecondTimestampTwo(Date date){  
	    if (null == date) {  
	        return 0;  
	    }  
	    String timestamp = String.valueOf(date.getTime()/1000);  
	    return Integer.valueOf(timestamp);  
	}  
}
